import { createRouter, createWebHistory } from 'vue-router'
import HomeView from '../views/HomeView.vue'
import { useLoginStore } from '@/stores/login'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      meta:{
        title:"仪表盘",
        requiresAuth:true
      },
      component: HomeView
    },
    {
      path: '/list',
      name: 'teacherList',
      meta:{
        title:"讲师列表",
        requiresAuth:true
      },
      component:()=>import("../views/teacher/list.vue")
    },
    {
      path: '/add',
      name: 'teacherFrom',
      meta:{
        title:"讲师添加",
        requiresAuth:true
      },
      component:()=>import("../views/teacher/from.vue")
    },

    {
      path: '/subjectList',
      name: 'subjectList',
      meta:{
        title:"课程分类列表",
        requiresAuth:true
      },
      component:()=>import("../views/subject/subjectList.vue")
    },
    {
      path: '/import',
      name: 'import',
      meta:{
        title:"课程分类导入",
        requiresAuth:true
      },
      component:()=>import("../views/subject/import.vue")
    },
    {
      path: '/courseList',
      name: 'courseList',
      meta:{
        title:"课程管理列表",
        requiresAuth:true
      },
      component:()=>import("../views/course/couresList.vue")
    },
    
    {
      path: '/info',
      name: 'info',
      meta:{
        title:"课程添加",
        requiresAuth:true
      },
      component:()=>import("../views/course/info.vue")
    },
    {
      path: '/chapter/:id',
      name: 'chapter',
      meta:{
        title:"课程章节",
        requiresAuth:true
      },
      component:()=>import("../views/course/chapter.vue")
    },
    {
      path: '/publish/:id',
      name: 'publish',
      meta:{
        title:"课程发布",
        requiresAuth:true
      },
      component:()=>import("../views/course/publish.vue")
    },
    
    {
      path: '/bannerList',
      name: 'bannerList',
      meta:{
        title:"轮播图列表",
        requiresAuth:true
      },
      component:()=>import("../views/banner/bannerList.vue")
    },
    {
      path: '/bannerAdd',
      name: 'bannerAdd',
      meta:{
        title:"添加轮播图",
        requiresAuth:true
      },
      component:()=>import("../views/banner/bannerAdd.vue")
    },

    {
      path: '/essayList',
      name: 'essayList',
      meta:{
        title:"文章列表",
        requiresAuth:true
      },
      component:()=>import("../views/essay/essayView.vue")
    },
    {
      path: '/essayAdd',
      name: 'essayAdd',
      meta:{
        title:"添加文章",
        requiresAuth:true
      },
      component:()=>import("../views/essay/essayAddView.vue")
    },
    {
      path: '/userList',
      name: 'userList',
      meta:{
        title:"用户列表",
        requiresAuth:true
      },
      component:()=>import("../views/user/userList.vue")
    },
    {
      path: '/userAdd',
      name: 'userAdd',
      meta:{
        title:"用户添加",
        requiresAuth:true
      },
      component:()=>import("../views/user/userAdd.vue")
    },
    {
      path: '/orderList',
      name: 'orderList',
      meta:{
        title:"订单列表",
        requiresAuth:true
      },
      component:()=>import("../views/order/orderList.vue")
    },
    {
      path: '/orderAdd',
      name: 'orderAdd',
      meta:{
        title:"添加订单",
        requiresAuth:true
      },
      component:()=>import("../views/order/orderAdd.vue")
    },
  
    {
      path: '/prole',
      name: 'prole',
      meta:{
        title:"角色管理",
        requiresAuth:true
      },
      component:()=>import("../views/permission/Prole.vue")
    },
    {
      path: '/proleAdd',
      name: 'proleAdd',
      meta:{
        title:"角色添加",
        requiresAuth:true
      },
      component:()=>import("../views/permission/ProleAdd.vue")
    },
    {
      path:'/login',
      name:'login',
      meta:{
        title:"登录",
        requiresAuth:false,
      },
      component:()=>import("../views/LoginView.vue")
    },
    
  ]
})

router.beforeEach((to, from, next) => {
  const loginStore = useLoginStore();
  const userInfo = localStorage.getItem("userInfoClient");

  if (loginStore.userInfo || userInfo) {
      next();
  } else {
      if (to.path === "/login") {
          next();
      } else {
          next("/login");
      }
  }
});
export default router
